package com.alibaba.citrus.cr.mn.order.center.facade.function.check;

import com.alibaba.citrus.cr.common.enums.ordercenter.*;
import com.alibaba.citrus.cr.common.exception.FunctionException;
import com.alibaba.citrus.cr.common.util.DateUtils;
import com.alibaba.citrus.cr.mn.order.center.common.util.AssertUtils;
import com.alibaba.citrus.cr.mn.order.center.common.util.JsonUtils;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.BaseDataServiceAdapter;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.epoch.app.mnordercenter.model.dto.DictionaryResponse;
import com.epoch.app.mnordercenter.model.dto.SalesOrderAtomCreateLineRequest;
import com.epoch.app.mnordercenter.model.dto.SalesOrderAtomCreateRequest;
import com.epoch.app.mnordercenter.model.dto.SalesOrderBizCreateRequest;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomCreateResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author dongju.fyj
 * @date 2023/12/21
 * @version 1.0
 */
@Service("salesOrderGroupCreateParamsCheck")
public class SalesOrderGroupCreateParamsCheck extends AbstractOrderParamsCheckService<SalesOrderBizCreateRequest, OrderAtomCreateResponse> {

    private static final Log log = Log.getLogger(SalesOrderGroupCreateParamsCheck.class);

    @Resource
    private BaseDataServiceAdapter baseDataServiceAdapter;

    @Override
    public void paramsNotBlankCheck(List<SalesOrderBizCreateRequest> requestList, List<OrderAtomCreateResponse> responseList) {
        AssertUtils.isTrue(CollectionUtils.isEmpty(requestList), "OC-01-001-01-15-001", "单据列表");
        AssertUtils.isTrue(requestList.size() > 100, "OC-01-001-01-15-004", "单据列表", requestList.size(), "100");
        requestList.forEach(t -> {
            AssertUtils.isTrue(StringUtils.isBlank(t.getFromSys()), "OC-01-001-01-15-001", "来源系统编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getBusinessType()), "OC-01-001-01-15-001", "业态编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getBusinessUnitCode()), "OC-01-001-01-15-001", "业务单元编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getOrderChannelCode()), "OC-01-001-01-15-001", "订单渠道编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getSourceOrderNo()), "OC-01-001-01-15-001", "来源单号");
            AssertUtils.isTrue(StringUtils.isBlank(t.getSalesOrderType()), "OC-01-001-01-15-001", "订单类型编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getCustomerCode()), "OC-01-001-01-15-001", "客户售达方编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getShipToPartyCode()), "OC-01-001-01-15-001", "客户送达方编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getBusinessChannel()), "OC-01-001-01-15-001", "业务渠道编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getSalesOrganizationCode()), "OC-01-001-01-15-001", "销售组织编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getProductGroupCode()), "OC-01-001-01-15-001", "产品组编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getExpectedDeliveryDate()), "OC-01-001-01-15-001", "期望交货日期");
            AssertUtils.isTrue(StringUtils.isBlank(t.getDeliveryMethodCode()), "OC-01-001-01-15-001", "配送方式编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getCreatorCode()), "OC-01-001-01-15-001", "创建人工号");

            AssertUtils.isTrue(CollectionUtils.isEmpty(t.getOrderDetails()), "OC-01-001-01-15-001", "单据明细");
            AssertUtils.isTrue(t.getOrderDetails().size() > 100, "OC-01-001-01-15-004", "单据明细", t.getOrderDetails().size(), "100");
            if (SalesOrderTypeEnum.GROUP_FREE_ORDER.equals(SalesOrderTypeEnum.of(t.getSalesOrderType()))) {
                AssertUtils.isTrue(StringUtils.isBlank(t.getCostCenterCode()), "OC-01-001-01-15-009", "成本中心编码", "(免费销售订单)该字段必填");
            } else {
                AssertUtils.isTrue(StringUtils.isNotBlank(t.getCostCenterCode()), "OC-01-001-01-15-009", "成本中心编码", "非(免费销售订单)不能有值");
            }

            // 校验单据明细
            t.getOrderDetails().forEach(detail -> {
                AssertUtils.isTrue(StringUtils.isBlank(detail.getSourceLineNum()), "OC-01-001-01-15-001", "来源单行号");
                AssertUtils.isTrue(StringUtils.isBlank(detail.getProductCode()), "OC-01-001-01-15-001", "物料编码");
                AssertUtils.isTrue(StringUtils.isBlank(detail.getProductQuantity()), "OC-01-001-01-15-001", "销售数量");
                AssertUtils.isTrue(StringUtils.isBlank(detail.getSalesUnitCode()), "OC-01-001-01-15-001", "销售单位编码");
                AssertUtils.isTrue(StringUtils.isBlank(detail.getProductionDate()), "OC-01-001-01-15-001", "生产日期");
                if (StringUtils.isBlank(detail.getShippingWarehouseCode())) {
                    if (StringUtils.isBlank(detail.getDeliveryFactoryCode()) && StringUtils.isBlank(detail.getStockCode())) {
                        throw new FunctionException("OC-01-001-01-15-010", "交货逻辑仓库编码、交货工厂编码、交货库存地点编码");
                    }
                    AssertUtils.isTrue(StringUtils.isBlank(detail.getDeliveryFactoryCode()), "OC-01-001-01-15-001", "交货工厂编码");
                    AssertUtils.isTrue(StringUtils.isBlank(detail.getStockCode()), "OC-01-001-01-15-001", "交货库存地点编码");
                }
            });

            // 校验单据明细行号是否重复
            List<String> sameSourceLines = t.getOrderDetails().stream()
                    .map(SalesOrderAtomCreateLineRequest::getSourceLineNum)
                    .collect(Collectors.toMap(p -> p, p -> 1, Integer::sum))
                    .entrySet().stream().filter(entry -> entry.getValue() > 1)
                    .map(Map.Entry::getKey).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(sameSourceLines)) {
                throw new FunctionException("OC-01-001-01-15-002", "来源单行号", StringUtils.join(sameSourceLines, ","));
            }
        });

        // 校验来源单号是否重复
        List<String> sameSourceOrderNos = requestList.stream()
                .map(SalesOrderAtomCreateRequest::getSourceOrderNo)
                .collect(Collectors.toMap(t -> t, t -> 1, Integer::sum))
                .entrySet().stream().filter(entry -> entry.getValue() > 1)
                .map(Map.Entry::getKey).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(sameSourceOrderNos)) {
            throw new FunctionException("OC-01-001-01-15-002", "来源单号", StringUtils.join(sameSourceOrderNos, ","));
        }
    }

    @Override
    public void paramsLengthCheck(List<SalesOrderBizCreateRequest> requestList, List<OrderAtomCreateResponse> responseList) {
        requestList.forEach(t -> {
            AssertUtils.lengthCheck(t.getFromSys(), 50, true, "OC-01-001-01-15-004", "来源系统编码");
            AssertUtils.lengthCheck(t.getBusinessType(), 10, true, "OC-01-001-01-15-004", "业态编码");
            AssertUtils.isNumber(t.getBusinessType(), true, "OC-01-001-01-15-006", "业态编码");
            AssertUtils.lengthCheck(t.getBusinessUnitCode(), 50, true, "OC-01-001-01-15-004", "业务单元编码");
            AssertUtils.lengthCheck(t.getOrderChannelCode(), 50, true, "OC-01-001-01-15-004", "订单渠道编码");
            AssertUtils.lengthCheck(t.getSourceOrderNo(), 50, true, "OC-01-001-01-15-004", "来源单号");
            AssertUtils.lengthCheck(t.getPreOrderNo(), 50, false, "OC-01-001-01-15-004", "预报订单号");
            AssertUtils.lengthCheck(t.getSalesOrderType(), 10, true, "OC-01-001-01-15-004", "订单类型编码");
            // 在创建现金销售订单时，一次性客户名称字段是必填的，其他类型的销售订单是非必填
            boolean isCashOrder = SalesOrderTypeEnum.CASH_ORDER.equals(SalesOrderTypeEnum.of(t.getSalesOrderType()));
            AssertUtils.lengthCheck(t.getCashCustomerName(), 50, isCashOrder, "OC-01-001-01-15-004", "一次性客户名称");
            AssertUtils.lengthCheck(t.getCustomerCode(), 50, true, "OC-01-001-01-15-004", "客户售达方编码");
            AssertUtils.lengthCheck(t.getShipToPartyCode(), 50, true, "OC-01-001-01-15-004", "客户送达方编码");
            AssertUtils.lengthCheck(t.getReceiveWarehouseCode(), 50, false, "OC-01-001-01-15-004", "收货仓库编码");
            AssertUtils.lengthCheck(t.getReceiveFactoryCode(), 50, false, "OC-01-001-01-15-004", "收货工厂编码");
            AssertUtils.lengthCheck(t.getReceiveStockCode(), 50, false, "OC-01-001-01-15-004", "收货库存地点编码");
            AssertUtils.lengthCheck(t.getBusinessChannel(), 50, true, "OC-01-001-01-15-004", "业务渠道编码");
            AssertUtils.lengthCheck(t.getSalesOrganizationCode(), 50, true, "OC-01-001-01-15-004", "销售组织编码");
            AssertUtils.lengthCheck(t.getProductGroupCode(), 50, true, "OC-01-001-01-15-004", "产品组编码");
            AssertUtils.lengthCheck(t.getSalesChannelCode(), 50, false, "OC-01-001-01-15-004", "销售渠道编码");
            AssertUtils.lengthCheck(t.getSalesChannelName(), 50, false, "OC-01-001-01-15-004", "销售渠道名称");
            AssertUtils.lengthCheck(t.getSalesDepartmentCode(), 50, false, "OC-01-001-01-15-004", "销售部门编码");
            AssertUtils.lengthCheck(t.getSalesDepartmentName(), 50, false, "OC-01-001-01-15-004", "销售部门名称");
            AssertUtils.lengthCheck(t.getCustomerGrade(), 50, false, "OC-01-001-01-15-004", "客户等级");
            AssertUtils.lengthCheck(t.getCustomerGroup(), 50, false, "OC-01-001-01-15-004", "客户组");
            AssertUtils.lengthCheck(t.getFulfillmentPriority(), 10, false, "OC-01-001-01-15-004", "履约优先级");
            AssertUtils.lengthCheck(t.getFulfillmentTypeCode(), 50, false, "OC-01-001-01-15-004", "履约方式编码");

            AssertUtils.lengthCheck(t.getExpectedDeliveryDate(), 10, true, "OC-01-001-01-15-004", "期望交货日期");
            AssertUtils.isTrue(!DateUtils.isDateFormat(t.getExpectedDeliveryDate()), "OC-00-001-01-15-001", "期望交货日期");
            // AssertUtils.isTrue(DateUtils.formatDate(new Date()).compareTo(t.getExpectedDeliveryDate()) > 0, "OC-01-001-01-16-021");
            AssertUtils.lengthCheck(t.getDeliveryMethodCode(), 50, true, "OC-01-001-01-15-004", "配送方式编码");
            AssertUtils.lengthCheck(t.getCreatorCode(), 50, true, "OC-01-001-01-15-004", "创建人工号");
            AssertUtils.lengthCheck(t.getCustomerPurchaseOrderNo(), 50, false, "OC-01-001-01-15-004", "客户采购订单号");
            AssertUtils.lengthCheck(t.getCustomerPurchaseDate(), 10, false, "OC-01-001-01-15-004", "客户采购订单日期");
            if (StringUtils.isNotBlank(t.getCustomerPurchaseDate())) {
                AssertUtils.isTrue(!DateUtils.isDateFormat(t.getCustomerPurchaseDate()), "OC-00-001-01-15-001", "客户采购订单日期");
            }
            AssertUtils.lengthCheck(t.getCostCenterCode(), 50, false, "OC-01-001-01-15-004", "成本中心编码");
            AssertUtils.lengthCheck(t.getOrderReasonCode(), 32, false, "OC-01-001-01-15-004", "订单原因编码");
            AssertUtils.lengthCheck(t.getNote(), 255, false, "OC-01-001-01-15-004", "订单头备注");
            AssertUtils.lengthCheck(t.getBizFeatures(), 1024, false, "OC-01-001-01-15-004", "扩展字段");
            if (CollectionUtils.isNotEmpty(t.getOrderBizTag())) {
                AssertUtils.isTrue(t.getOrderBizTag().size() > 10, "OC-01-001-01-15-004", "订单标签集合", t.getOrderBizTag().size(), "10");
                for (String orderTag : t.getOrderBizTag()) {
                    AssertUtils.lengthCheck(orderTag, 50, "OC-01-001-01-15-004", "订单标签");
                }
            }
            t.getOrderDetails().forEach(detail -> {
                AssertUtils.lengthCheck(detail.getSourceLineNum(), 10, true, "OC-01-001-01-15-004", "来源单行号");
                AssertUtils.lengthCheck(detail.getProductCode(), 50, true, "OC-01-001-01-15-004", "物料编码");
                AssertUtils.isWholeNumber(detail.getProductQuantity(), true, "OC-01-001-01-15-009", "销售数量", "销售数量须为正整数");
                AssertUtils.lengthCheck(detail.getProductQuantity(), 10, true, "OC-01-001-01-15-004", "销售数量");
                AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getProductQuantity())) >= 0, "OC-01-001-01-15-009", "销售数量", "销售数量须为正整数");
                AssertUtils.isTrue(new BigDecimal("2147483647").compareTo(new BigDecimal(detail.getProductQuantity())) < 0, "OC-01-001-01-15-009", "销售数量", "最大2147483647");
                AssertUtils.lengthCheck(detail.getSalesUnitCode(), 50, true, "OC-01-001-01-15-004", "销售单位编码");
                AssertUtils.lengthCheck(detail.getShippingWarehouseCode(), 50, false, "OC-01-001-01-15-004", "发货逻辑仓库编码");
                AssertUtils.lengthCheck(detail.getDeliveryFactoryCode(), 50, false, "OC-01-001-01-15-004", "发货工厂编码");
                AssertUtils.lengthCheck(detail.getStockCode(), 50, false, "OC-01-001-01-15-004", "发货库存地点编码");
                AssertUtils.isAmountNumber(detail.getManualTotalAmount(), false, "OC-01-001-01-15-006", "手工总金额");
                AssertUtils.lengthCheck(detail.getManualTotalAmount(), 10, false, "OC-01-001-01-15-004", "手工总金额");
                AssertUtils.moneyCheck(detail.getManualTotalAmount(), false, "OC-01-001-01-15-005", "手工总金额");
                if (StringUtils.isNotBlank(detail.getManualTotalAmount())) {
                    AssertUtils.isTrue(new BigDecimal("2147483647").compareTo(new BigDecimal(detail.getManualTotalAmount())) < 0, "OC-01-001-01-15-009", "手工总金额", "最大2147483647");
                }
                AssertUtils.lengthCheck(detail.getNote(), 255, false, "OC-01-001-01-15-004", "订单行备注");
                AssertUtils.lengthCheck(detail.getBizFeatures(), 1024, false, "OC-01-001-01-15-004", "扩展字段");

                // 确认数量
                if (StringUtils.isNotBlank(detail.getConfirmQuantity())) {
                    AssertUtils.isWholePositiveNumber(detail.getConfirmQuantity(), true, "OC-01-001-01-15-009", "确认数量", "确认数量须为大于等于0的整数");
                    AssertUtils.lengthCheck(detail.getConfirmQuantity(), 10, true, "OC-01-001-01-15-004", "确认数量");
                    AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getConfirmQuantity())) > 0, "OC-01-001-01-15-009", "确认数量", "确认数量须为大于等于0的整数");
                    AssertUtils.isTrue(new BigDecimal("2147483647").compareTo(new BigDecimal(detail.getConfirmQuantity())) < 0, "OC-01-001-01-15-009", "确认数量", "最大2147483647");
                }

                // 赠品数量
                AssertUtils.isWholePositiveNumber(detail.getGiftQuantity(), false, "OC-01-001-01-15-009", "赠品数量", "赠品数量须为大于等于0的整数");
                AssertUtils.lengthCheck(detail.getGiftQuantity(), 10, false, "OC-01-001-01-15-004", "赠品数量");
                if (StringUtils.isNotBlank(detail.getGiftQuantity())) {
                    AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getGiftQuantity())) > 0, "OC-01-001-01-15-009", "赠品数量", "赠品数量须为大于等于0的整数");
                    AssertUtils.isTrue(new BigDecimal("2147483647").compareTo(new BigDecimal(detail.getGiftQuantity())) < 0, "OC-01-001-01-15-009", "赠品数量", "最大2147483647");
                }

                // 税率
                AssertUtils.isWholePositiveNumber(detail.getTaxRate(), false, "OC-01-001-01-15-009", "税率", "税率须为大于等于0，小于等于100的整数");
                AssertUtils.lengthCheck(detail.getTaxRate(), 10, false, "OC-01-001-01-15-004", "税率");
                if (StringUtils.isNotBlank(detail.getTaxRate())) {
                    AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getTaxRate())) > 0, "OC-01-001-01-15-009", "税率", "税率须大于0的整数");
                    AssertUtils.isTrue(new BigDecimal("100").compareTo(new BigDecimal(detail.getTaxRate())) < 0, "OC-01-001-01-15-009", "税率", "税率须小于100的整数");
                }

                // 生产日期
                AssertUtils.lengthCheck(detail.getProductionDate(), 10, true, "OC-01-001-01-15-004", "生产日期");
                if (StringUtils.isNotBlank(detail.getProductionDate())) {
                    AssertUtils.isTrue(!DateUtils.isDateFormat(detail.getProductionDate()), "OC-00-001-01-15-001", "生产日期");
                }

                // 折扣后总金额
//                if (StringUtils.isNotBlank(detail.getActivityTotalAmount())) {
//                    AssertUtils.isAmountNumber(detail.getActivityTotalAmount(), false, "OC-01-001-01-15-006", "折扣后总金额");
//                    AssertUtils.lengthCheck(detail.getActivityTotalAmount().split("\\.")[0], 10, false, "OC-01-001-01-15-004", "折扣后总金额");
//                    AssertUtils.moneyCheck(detail.getActivityTotalAmount(), false, "OC-01-001-01-15-005", "折扣后总金额");
//                    AssertUtils.isTrue(new BigDecimal("2147483647").compareTo(new BigDecimal(detail.getActivityTotalAmount())) < 0, "OC-01-001-01-15-009", "折扣后总金额", "最大2147483647");
//                }

            });
        });
    }

    @Override
    public void paramsEnumDictCheck(List<SalesOrderBizCreateRequest> requestList, List<OrderAtomCreateResponse> responseList) {
        // 系统来源
        List<DictionaryResponse> fromSysList = baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.FROM_SYS.getCode());
        Map<String, DictionaryResponse> fromSysMap = fromSysList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
        // 业态
        List<DictionaryResponse> businessTypeList = baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.BUSINESS_TYPE.getCode());
        Map<String, DictionaryResponse> businessTypeMap = businessTypeList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
        // 业务单元编码
        List<DictionaryResponse> businessUnitCodeList = baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.BUSINESS_UNIT_CODE.getCode());
        Map<String, DictionaryResponse> businessUnitCodeMap = businessUnitCodeList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
        log.info("businessUnitCodeMap结果:{}", JSON.toJSONString(businessUnitCodeMap));
        //履约方式
        List<DictionaryResponse> fulfillmentTypeList = baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.GROUP_SALE_FULFILLMENT_TYPE.getCode());
        Map<String, DictionaryResponse> fulfillmentTypeMap = fulfillmentTypeList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));

        requestList.forEach(t -> {
            // 校验来源系统编码
            if (Objects.isNull(fromSysMap.get(t.getFromSys()))) {
                throw new FunctionException("OC-01-001-01-15-006", "来源系统编码");
            }
            // 校验业态编码
            DictionaryResponse dictionaryResponse = businessTypeMap.get(t.getBusinessType());
            if (Objects.isNull(dictionaryResponse)) {
                throw new FunctionException("OC-01-001-01-15-006", "业态编码");
            }
            t.setBusinessTypeName(dictionaryResponse.getName());
            // 校验业务单元编码
            dictionaryResponse = businessUnitCodeMap.get(t.getBusinessUnitCode());
            log.info("dictionaryResponse结果:{}",JSON.toJSONString(dictionaryResponse));
            if (Objects.isNull(dictionaryResponse)) {
                throw new FunctionException("OC-01-001-01-15-006", "业务单元编码");
            }
            t.setBusinessUnitName(dictionaryResponse.getName());
            // 校验订单渠道编码
            if (StringUtils.isNotBlank(t.getOrderChannelCode())) {
                OrderChannelEnum orderChannelEnum = OrderChannelEnum.of(t.getOrderChannelCode());
                Optional.ofNullable(orderChannelEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "订单渠道编码"));
                t.setOrderChannelName(orderChannelEnum.getDesc());
            }

            // 校验订单类型编码
            SalesOrderTypeEnum salesOrderTypeEnum = SalesOrderTypeEnum.groupOrderOf(t.getSalesOrderType());
            Optional.ofNullable(salesOrderTypeEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "订单类型编码"));
            t.setSalesOrderTypeName(salesOrderTypeEnum.getDesc());

            // 校验配送方式编码
            SalesOrderDeliveryMethodEnum salesOrderDeliveryMethodEnum = SalesOrderDeliveryMethodEnum.of(t.getDeliveryMethodCode());
            Optional.ofNullable(salesOrderDeliveryMethodEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "配送方式编码"));
            t.setDeliveryMethodName(salesOrderDeliveryMethodEnum.getDesc());

            // 校验订单原因编码
            if (StringUtils.isNotBlank(t.getOrderReasonCode())) {
                SalesOrderReasonEnum salesOrderReasonEnum = SalesOrderReasonEnum.of(t.getOrderReasonCode());
                Optional.ofNullable(salesOrderReasonEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "订单原因编码"));
            }

            if (StringUtils.isNotBlank(t.getCustomerGroup())){
                CustomerGroupEnum customerGroupEnum = CustomerGroupEnum.of(t.getCustomerGroup());
                if (Objects.nonNull(customerGroupEnum)) {
                    // 设置客户分组名称
                    t.setCustomerGroupName(customerGroupEnum.getDesc());
                }
            }

            // 校验扩展字段是否为JSON格式
            AssertUtils.isTrue(!JsonUtils.isValidJson(t.getBizFeatures()), "OC-00-001-01-15-001", "扩展字段");

            for (SalesOrderAtomCreateLineRequest p : t.getOrderDetails()) {
                StockUnitEnum salesUnit = StockUnitEnum.of(p.getSalesUnitCode());
                Optional.ofNullable(salesUnit).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "销售单位编码"));
                p.setSalesUnitName(salesUnit.getDesc());

                // 校验扩展字段是否为JSON格式
                AssertUtils.isTrue(!JsonUtils.isValidJson(p.getBizFeatures()), "OC-00-001-01-15-001", "扩展字段");
            }

            // 订单标签校验
            if (CollectionUtils.isNotEmpty(t.getOrderBizTag())) {
                for (String orderTag : t.getOrderBizTag()) {
                    AssertUtils.isTrue(!OrderBizTagEnum.codes().contains(orderTag), "OC-01-001-01-15-006", "订单标签");
                }
            }

            if (StringUtils.isNotBlank(t.getFulfillmentTypeCode())) {
                AssertUtils.isTrue(Objects.isNull(fulfillmentTypeMap.get(t.getFulfillmentTypeCode())), "OC-01-001-01-15-006", "履约方式编码");
            }

            if (StringUtils.isNotBlank(t.getFulfillmentPriority())) {
                AssertUtils.isTrue(Objects.isNull(FulfillmentPriorityEnum.of(t.getFulfillmentPriority())), "OC-01-001-01-15-006", "履约优先级");
            }

        });
    }
}
